package com.gxc.blogm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.gxc.authcore.system.util.CurrentUtil;
import com.gxc.blogm.domain.Article;
import com.gxc.blogm.service.ArticleService;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.RequestUtil;
import com.gxc.webcore.util.StringUtil;

public class ArticleController {

	ArticleService articleService;

	public ArticleService getArticleService() {
		return articleService;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	/**
	 * 列表页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("blogm/article/article_list.vm");
	}

	/**
	 * 列出一页文章
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelAndView grid(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = RequestUtil.getInMap(request);
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		PageList pageList = articleService.listPage(map, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 新建文章页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView create(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map model = new HashMap();
		// Map params = new HashMap();
		// params.put("parent_code", "1");
		// params.put("fieldSort", "code asc");
		// List article_type_list = dicService.listAll(params);
		// model.put("article_type_list", article_type_list);
		return new ModelAndView("blogm/article/article_create.vm", model);
	}

	/**
	 * 新建文章保存
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView createSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = new HashMap();
		Article article = (Article) RequestUtil.getInObject(request,
				Article.class);
		if (article.getTitle() == null || article.getTitle().trim().equals("")
				|| article.getContent() == null
				|| article.getContent().trim().equals("")) {
			map.put("status", "failed");
			map.put("data", "标题和文章内容不能为空。");
		} else {
			article.setId("article_" + StringUtil.getUUID());
			article.setSummary(articleService.getSummary(article.getContent()));
			article.setComment_num(0);
			article.setView_num(0);
			article.setCreate_user(CurrentUtil.getAuthUser().getId());
			article.setCreate_time(new Date());
			article.setUpdate_time(new Date());
			int i = articleService.create(article);
			map.put("status", "success");
			map.put("data", i);
		}
		String result = JsonUtil.object2json(map);
		response.getWriter().write(result);
		return null;
	}

	/**
	 * 修改文章页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map model = new HashMap();
		String id = request.getParameter("id");
		Article article = articleService.retrieve(id);
		model.put("article", article);
		// Map params = new HashMap();
		// params.put("parent_code", "1");
		// params.put("fieldSort", "code");
		// List article_type_list = dicService.listAll(params);
		// model.put("article_type_list", article_type_list);
		return new ModelAndView("blogm/article/article_edit.vm", model);
	}

	/**
	 * 修改文章保存
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView editSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		Article article = (Article) RequestUtil.getInObject(request,
				Article.class);
		article.setUpdate_time(new Date());
		article.setSummary(articleService.getSummary(article.getContent()));
		int i = articleService.update(article);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 删除一个文章
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		int i = articleService.delete(id);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 删除多个文章
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView deleteBatch(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		String[] id_array = id.split(",");
		int i = articleService.delete(id_array);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 查看文章
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Article article = articleService.retrieve(id);
		Map model = new HashMap();
		model.put("article", article);
		return new ModelAndView("blogm/article/article_view.vm", model);
	}

}
